import 'package:flutter/material.dart';
import '../utils/adapt.dart';
import '../utils/request.dart';
import 'package:provider/provider.dart';
import '../provider/playAudioList.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class AlbumPage extends StatefulWidget {
  var arguments;
  AlbumPage({Key? key, this.arguments}) : super(key: key);

  @override
  _AlbumPageState createState() => _AlbumPageState(this.arguments);
}

class _AlbumPageState extends State<AlbumPage> {
  var arguments;
  _AlbumPageState(this.arguments);
  String _topImag =
      "https://p2.music.126.net/klOSGBRQhevtM6c9RXrM1A==/18808245906527670.jpg?param=300y200";
  String _title = '';
  List _list = [];
  String _decription = '';
  void _getData(id) async {
    EasyLoading.show(status: 'loading...');
    var res = await getHttp('/album?id=${id}');
    EasyLoading.dismiss();
    this.setState(() {
      _topImag = res['album']['picUrl'];
      _list = res['songs'];
      _decription = res['album']['description'];
      _title = res['album']['name'];
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getData(arguments['albumId']);
  }

  @override
  Widget build(BuildContext context) {
    var playAudioList = Provider.of<PlayAudioList>(context);
    return CustomScrollView(
      slivers: [
        SliverAppBar(
          pinned: true,
          snap: true,
          floating: true,
          expandedHeight: Adapt.px(400),
          flexibleSpace: FlexibleSpaceBar(
            title: Text("$_title"),
            background: Image.network(
              _topImag + '?param=300y200',
              fit: BoxFit.cover,
            ),
          ),
        ),
        SliverToBoxAdapter(
          child: Container(
            padding: EdgeInsets.all(Adapt.px(20)),
            child: Text(
              "$_decription",
              style: TextStyle(fontSize: Adapt.px(30)),
            ),
          ),
        ),
        SliverList(
          delegate: SliverChildBuilderDelegate((content, index) {
            return Container(
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(width: 1, color: Colors.grey),
                ),
              ),
              child: ListTile(
                onTap: () {
                  var newList = _list
                      .map((item) => {
                            "id": item['id'],
                            "name": item['name'],
                            "picUrl": item['al']['picUrl']
                          })
                      .toList();
                  playAudioList.setCurrentIndex(index);
                  playAudioList.setAudioList(newList);
                  Navigator.pushNamed(context, "/audioplayer", arguments: {});
                },
                leading: Image.network(
                  _list[index]['al']['picUrl'],
                  width: Adapt.px(100),
                  height: Adapt.px(100),
                ),
                title: Text(_list[index]['name']),
                trailing: Icon(Icons.play_circle_fill_rounded),
              ),
            );
          }, childCount: _list.length),
        ),
      ],
    );
  }
}
